package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.User;
import dao.DaoHelper;
import dao.UserDao;

public class UserDaoImp implements UserDao {
	
	private static UserDaoImp userDao = new UserDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private UserDaoImp(){}

	public static UserDaoImp getInstance()
	{
		return userDao;
	}

	@Override
	public User find(String id, String password){
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		User user = null;

		try {	
			stmt=con.prepareStatement("select * from user where id = ? and password = ?");
			stmt.setString(1, id);
			stmt.setString(2, password);
			result=stmt.executeQuery();
			if (result.next()) {
				user = new User(id, password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}
		return user;
	}

}
